package com.community.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

import com.community.common.core.redis.RedisCache;
import com.community.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.community.system.mapper.VehicleRecordMapper;
import com.community.system.domain.VehicleRecord;
import com.community.system.service.IVehicleRecordService;

/**
 * recordService业务层处理
 *
 * @author ruoyi
 * @date 2025-10-27
 */
@Service
public class VehicleRecordServiceImpl implements IVehicleRecordService {
    @Autowired
    private VehicleRecordMapper vehicleRecordMapper;
    @Autowired
    private RedisCache redisCache;


    /**
     * 查询record
     *
     * @param recordId record主键
     * @return record
     */
    @Override
    public VehicleRecord selectVehicleRecordByRecordId(Long recordId) {
        return vehicleRecordMapper.selectVehicleRecordByRecordId(recordId);
    }

    /**
     * 查询record列表
     *
     * @param vehicleRecord record
     * @return record
     */
    @Override
    public List<VehicleRecord> selectVehicleRecordList(VehicleRecord vehicleRecord) {
        return vehicleRecordMapper.selectVehicleRecordList(vehicleRecord);
    }

    /**
     * 新增record
     *
     * @param vehicleRecord record
     * @return 结果
     */
    @Override
    public int insertVehicleRecord(VehicleRecord vehicleRecord) {
        vehicleRecord.setCreateTime(DateUtils.getNowDate());
        return vehicleRecordMapper.insertVehicleRecord(vehicleRecord);
    }

    /**
     * 修改record
     *
     * @param vehicleRecord record
     * @return 结果
     */
    @Override
    public int updateVehicleRecord(VehicleRecord vehicleRecord) {
        vehicleRecord.setUpdateTime(DateUtils.getNowDate());
        return vehicleRecordMapper.updateVehicleRecord(vehicleRecord);
    }

    /**
     * 批量删除record
     *
     * @param recordIds 需要删除的record主键
     * @return 结果
     */
    @Override
    public int deleteVehicleRecordByRecordIds(Long[] recordIds) {
        return vehicleRecordMapper.deleteVehicleRecordByRecordIds(recordIds);
    }

    /**
     * 删除record信息
     *
     * @param recordId record主键
     * @return 结果
     */
    @Override
    public int deleteVehicleRecordByRecordId(Long recordId) {
        return vehicleRecordMapper.deleteVehicleRecordByRecordId(recordId);
    }

    /**
     * 车辆入场
     *
     * @param vehicleRecord record
     * @return 结果
     */
    @Override

    public int insertVehicleRecordIn(VehicleRecord vehicleRecord) {
        //设置入场时间
        vehicleRecord.setEntryTime(DateUtils.getNowDate());
        //设置状态
        vehicleRecord.setStatus("在场");

        //更新redis中的车辆状态
        String CarNo = vehicleRecord.getCarNo();
        //缓存车辆状态
        String redisKey = "sys-business:vehicle:status:" + CarNo;
        redisCache.setCacheObject(
                redisKey,
                new HashMap<String, Object>() {{
                    put("status", vehicleRecord.getStatus());
                    put("entryTime", vehicleRecord.getEntryTime());
                    put("communityId", vehicleRecord.getCommunityId());
                    put("recordId", vehicleRecord.getRecordId());
                }}
        );


        return vehicleRecordMapper.updateVehicleRecord(vehicleRecord);
    }









    /**
     * 车辆离场
     *
     * @param vehicleRecord record
     * @return 结果
     */
    @Override
    public int insertVehicleRecordOut(VehicleRecord vehicleRecord) {
        //设置离场时间
        vehicleRecord.setExitTime(DateUtils.getNowDate());
        //设置状态
        vehicleRecord.setStatus("离场");
        //更新redis中的车辆状态
        String CarNo = vehicleRecord.getCarNo();
        String redisKey = "sys-business:vehicle:status:" + CarNo;

        //设置款项
        //根据入场时间和离场时间计算时长
        long duration = vehicleRecord.getExitTime().getTime() - vehicleRecord.getEntryTime().getTime();
        //根据时长计算费用
        BigDecimal fee = BigDecimal.valueOf(duration).divide(BigDecimal.valueOf(1000 * 60 * 60), 2, RoundingMode.HALF_UP);
        vehicleRecord.setFee(fee);

        //删除对应key的redis缓存数据
        redisCache.deleteObject(redisKey);

        //将数据写入sql
        int result = vehicleRecordMapper.updateVehicleRecord(vehicleRecord);

        return result;
    }


}
